#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

// 非阻塞的轮询模型
int main()
{
  pid_t ret = fork();

  // child
  if (ret == 0)
  {
    int count = 3;
    while(count--)
    {
      printf("child do some things\n");
      sleep(3);
    }

    exit(0);
  }


  // father
  while(1)
  { 
    int status = 0;
    pid_t id = waitpid(ret , &status , WNOHANG );
    if (id == 0)
    {
      printf("father do some things\n");
      sleep(1);
    }
    else if (id > 0) // wait success!!
    {
      printf("father wait child success! id:%d\n",ret);
      break;
    }
    else 
    {
      printf("child is killed sign is:%d\n", status&0x7f);
    }
  }

  return 0;
}
